home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / fax / src / sgi2fax / lut.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  3KB  |  107 lines

  1. /*    $Header: /usr/people/sam/fax/sgi2fax/RCS/lut.c,v 1.5 1994/02/28 14:23:11 sam Rel $
  2. /*
  3.  * Copyright (c) 1990, 1991, 1992, 1993, 1994 Sam Leffler
  4.  * Copyright (c) 1991, 1992, 1993, 1994 Silicon Graphics, Inc.
  5.  *
  6.  * Permission to use, copy, modify, distribute, and sell this software and 
  7.  * its documentation for any purpose is hereby granted without fee, provided
  8.  * that (i) the above copyright notices and this permission notice appear in
  9.  * all copies of the software and related documentation, and (ii) the names of
  10.  * Sam Leffler and Silicon Graphics may not be used in any advertising or
  11.  * publicity relating to the software without the specific, prior written
  12.  * permission of Sam Leffler and Silicon Graphics.
  13.  * 
  14.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
  15.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
  16.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
  17.  * 
  18.  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  19.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  20.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  21.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
  22.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
  23.  * OF THIS SOFTWARE.
  24.  */
  25. #include "lut.h"
  26.  
  27. float frand();
  28.  
  29. lut *makelut(func,insteps,outsteps,stoclut)
  30. float (*func)();
  31. int insteps, outsteps, stoclut;
  32. {
  33.     lut *l;
  34.     int i;
  35.     float low, high, inspace;
  36.  
  37.     l = (lut *)malloc(sizeof(lut));
  38.     l->insteps = insteps;
  39.     l->outsteps = outsteps;
  40.     l->stoclut = stoclut;
  41.     if(stoclut) {
  42.         l->flow = (float *)malloc(insteps*sizeof(float));
  43.         l->fhigh = (float *)malloc(insteps*sizeof(float));
  44.         inspace = insteps-1.0;
  45.         for(i=0; i<insteps; i++) {
  46.             low = (i-0.5)/inspace;
  47.             high = (i+0.5)/inspace;
  48.             if(low<0.0)
  49.                 low = 0.0;
  50.             if(high>1.0)
  51.                 high = 1.0;
  52.             l->flow[i] = (func)(low);
  53.             l->fhigh[i] = (func)(high);
  54.         }
  55.     } else { 
  56.         l->stab = (unsigned short *)malloc(insteps*sizeof(unsigned short));
  57.         inspace = insteps-1.0;
  58.         for(i=0; i<insteps; i++) 
  59.             l->stab[i] = (l->outsteps-1)*(func)(i/inspace)+0.5;
  60.     }
  61.     return l;
  62. }
  63.  
  64. applylut(l,sptr,n)
  65. lut *l;
  66. unsigned short *sptr;
  67. int n;
  68. {
  69.     float delta, val;
  70.     float *fhigh, *flow;
  71.     unsigned short *stab;
  72.     float outspace;
  73.     int ival;
  74.  
  75.     if(l->stoclut) {
  76.         fhigh = l->fhigh;
  77.         flow = l->flow;
  78.         outspace = l->outsteps-1;
  79.         while(n--) {
  80.             delta = fhigh[*sptr]-flow[*sptr];
  81.             val = outspace*(flow[*sptr]+frand()*delta);
  82.             ival = val;
  83.             if((val-ival)<frand()) 
  84.                 *sptr++ = ival;
  85.             else
  86.                 *sptr++ = ival+1;
  87.         }
  88.     } else {
  89.         stab = l->stab;
  90.         while(n--) {
  91.             *sptr = stab[*sptr];
  92.             sptr++;
  93.         }
  94.     }
  95. }
  96.  
  97. void freelut(lut* l)
  98. {
  99.     if(l->stoclut) {
  100.         free((char*)l->flow);
  101.         free((char*)l->fhigh);
  102.     } else { 
  103.         free((char*)l->stab);
  104.     }
  105.     free((char*)l);
  106. }
  107.